package com.example.xml

class XmlParserDemo {
	
	static void main(String[] args) {
		
		//创建xmlPaser文件，并解析文件。doc对象即为 xml文件的根节点
		def parser = new XmlParser()
        def doc = parser.parse("d://groovyTest/students2.xml"); //这里相当于指针已经指向根节点了
//		def doc2 = parser.parseText(xmlString);//解析xml字符串,就不是从文件里面读取
		
		/**
		 * <studentList createTime='Sun Jun 23 23:05:01 CST 2019'>
			  <student>
			    <id>1263781</id>
			    <name>assad</name>
			    <age>20</age>
			    <city>Guangzhou</city>
			  </student>
			  <student>
			    <id>3729321</id>
			    <name>Kim</name>
			    <age>21</age>
			    <city>Shenzhen</city>
			  </student>
			</studentList>
		 */
		//获取第一个student的name
		def name = doc.student[0].name.text();
		//获取studentsList的时间
		def createDate = doc['@createTime'];//获取到时间
		
		println name 
		println createDate
		
		//用lamda表达式
		doc.student.each{
			e -> println("id:${e.id.text()},${e.name.text()},${e.age.text()},${e.city.text()}")
		}
		
		print('===========用迭代器来变量解析之后的xml=========')
		//用迭代器
		def iter = doc.iterator();
		println(iter.next().id.text())
//		println(iter.name())
		while(iter.hasNext()) {
			def e = iter.next();
			println("id:${e.id.text()},${e.name.text()},${e.age.text()},${e.city.text()}")
		}
		
		
	}
}
